package com.css.dzj.dzjfxyj.modules.over.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjfxyj.common.audit.service.SysAuditService;
import com.css.dzj.dzjfxyj.common.util.Constant;
import com.css.dzj.dzjfxyj.common.util.HttpClientUtil;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.common.util.ServiceUtil;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.distribute.entity.JgFxyjFenfaEntity;
import com.css.dzj.dzjfxyj.modules.distribute.service.DistributeService;
import com.css.dzj.dzjfxyj.modules.over.service.OverServcie;
import com.css.dzj.dzjfxyj.modules.over.vo.OverVo;
import com.css.dzj.dzjfxyj.modules.reply.entitiy.JgFxyjFeedbackEntity;
import com.css.dzj.dzjfxyj.modules.reply.vo.ReplyVo.MainDistributeVo;
import com.css.dzj.dzjfxyj.modules.reply.vo.ReplyVo.ReplyVo;
import com.google.gson.JsonObject;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 结束
 */
@RestController
@RequestMapping("/over")
public class OverController {
    @Autowired
    OverServcie overServcie;

    @Autowired
    DistributeService distributeService;

    @Value("${AP_URL}")
    private String apUrl;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 分页查询结束信息
     *
     * @param pageSize
     * @param pageNo
     * @param fxyjDomainId
     * @param fxyjTitle
     * @param fxyjLevel
     * @param fxyjObjectName
     * @param startTime
     * @param endTime
     * @param startTime1
     * @param endTime1
     * @return
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam(name = "pageSize", required = false) Integer pageSize,
                     @RequestParam(name = "pageNo", required = false) Integer pageNo,
                     @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                     @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                     @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                     @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                     @RequestParam(name = "startTime", required = false) String startTime,
                     @RequestParam(name = "endTime", required = false) String endTime,
                     @RequestParam(name = "startTime1", required = false) String startTime1,
                     @RequestParam(name = "endTime1", required = false) String endTime1, HttpServletRequest request) {

        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults<MainDistributeVo> page = overServcie.getPage(pageRequest, fxyjDomainId, fxyjTitle, fxyjLevel,
                fxyjObjectName, startTime, endTime, startTime1, endTime1);
        //baseService.logSuccess(3,"结束查询成功","2");
        String s = JSON.toJSONString(page);
        sysAuditService.saveSuccessAudit("分页查询结束信息成功", Constant.AUDIT_MODULE_OVER, Constant.PT, Constant.AUDIT_TYPE_PAGE, s, request);
        return R.ok().put("data", page);
    }

    /***
     * 保存结果
     * @param replyVo
     * @param request
     * @return
     */
    @PostMapping("/saveOver")
    public R saveOver(@RequestBody ReplyVo replyVo, HttpServletRequest request) {
        try {
            List<JgFxyjFeedbackEntity> list = replyVo.getRiskwarningChildren();
            for (JgFxyjFeedbackEntity jgFxyjFeedbackEntity : list) {
                overServcie.saveOver(jgFxyjFeedbackEntity, replyVo.getRiskwarning().getId(), request);
            }
            //baseService.logSuccess(3,"保存结束成功","2");
            String s = JSON.toJSONString(replyVo);
            sysAuditService.saveSuccessAudit("保存结果成功", Constant.AUDIT_MODULE_OVER, Constant.PT, Constant.AUDIT_TYPE_ADD, s, request);
            return R.ok("结束成功");
        } catch (Exception e) {
            //baseService.logFailed(3,"保存结束失败","2");
            String s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("保存结果失败", Constant.AUDIT_MODULE_OVER, Constant.YZ, Constant.AUDIT_TYPE_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 查看详情
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public R findById(@RequestParam String id, HttpServletRequest request) {
        if (!StringUtils.isEmpty(id)) {
            ReplyVo overVo = new ReplyVo();
            List<JgFxyjFeedbackEntity> list = new ArrayList<>();
            JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity = overServcie.findById(id);
            String orgName = ServiceUtil.getOrgName(request);
            if(!StringUtils.isEmpty(orgName)) {
                JgFxyjFenfaEntity jgFxyjFenfaEntity = distributeService.findByGetIdAndFenfaToOrg(id, orgName);
                JgFxyjFeedbackEntity jgFxyjFeedbackEntity = overServcie.findFeebackById(jgFxyjFenfaEntity.getId());
                list.add(jgFxyjFeedbackEntity);
            }
//            List<JgFxyjFenfaEntity> jgFxyjFenfaEntity = overServcie.findDistrById(id);
            overVo.setRiskwarning(jgFxyjWarningSignalEntity);
            overVo.setRiskwarningChildren(list);
            //baseService.logSuccess(3,"查询成功","2");
            String s = JSON.toJSONString(overVo);
            sysAuditService.saveSuccessAudit("查看详情成功", Constant.AUDIT_MODULE_OVER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
            return R.ok().put("data", overVo);
        } else {
            //baseService.logFailed(3,"查询失败","2");
            String s = JSON.toJSONString(null);
            RuntimeException e = new RuntimeException("ID为空");
            sysAuditService.saveFailAudit("查看详情失败", Constant.AUDIT_MODULE_OVER, Constant.YZ, Constant.AUDIT_TYPE_SELECT, s, e, request);
            return R.error("id不能为空");
        }
    }

    /**
     * 去安评里调接口
     *
     * @param code
     * @return
     */
    @GetMapping("/getAction")
    public R getAction(@RequestParam String code) {
        String msg = HttpClientUtil.doGet(apUrl + "/check/getAction?code=" + code);
        if (StringUtils.isEmpty(msg)) {
            return R.error("没有对应数据！");
        }
        JSONObject jsonObject = JSON.parseObject(msg);
        Object data = jsonObject.get("data");
        if (StringUtils.isEmpty(data)) {
            return R.error("没有对应数据！");
        }
        //baseService.logSuccess(3,"查询行政行为成功","2");
        String s = JSON.toJSONString(data);
        sysAuditService.saveSuccessAudit("去安评里调接口成功", Constant.AUDIT_MODULE_OVER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
        return R.ok().put("data", data);
    }
}
